package ysq.xyg.wxapi.util;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.iherus.codegen.qrcode.SimpleQrcodeGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ysq.xyg.base.util.ProProperties;

import java.io.FileOutputStream;
import java.io.OutputStream;

/**
 * 利用二维码生成器
 *
 * @author taocongcong
 * @create 2017-09-04 9:37
 */
public class QrcodeUtil {
    private static final Logger log= LoggerFactory.getLogger(QrcodeUtil.class);
    /**
     * 条形码编码
     * @param contents
     * @param width
     * @param height
     * @param imgPath
     */
    public static void encode(String contents, int width, int height, String imgPath) {
        //保证最小为70*25的大小
        int codeWidth = Math.max(70, width);
        int codeHeight = Math.max(25, height);
        try {
            //使用EAN_13编码格式进行编码
            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
                    BarcodeFormat.CODE_39, codeWidth, codeHeight, null);
            //生成png格式的图片保存到imgPath路径
            MatrixToImageWriter.writeToStream(bitMatrix, "png",
                    new FileOutputStream(imgPath));
            System.out.println("encode success! the img's path is "+imgPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 只生成纯二维码
     */
    public static String qrcode(String url) throws Exception {
        OutputStream out = null;

        try {
            String newUrl = FilePathUtil.setFilePath();
            out = new FileOutputStream(newUrl);
            // 生成图片的绝对引用地址
            String picUrl = StringUtils.replace(StringUtils.substringAfter(newUrl, ProProperties.getInstance().getBaseUploadURL()),"\\", "/");
            new SimpleQrcodeGenerator().generate(url).toStream(out);
            return picUrl;

        } finally {
            IOUtils.closeQuietly(out);
        }
    }
    /**
     * 只生成纯二维码
     */
    public static String qrcode(String url,String path) throws Exception {
        log.info("url is "+url);
        log.info("path is "+path);
        OutputStream out = null;
        try {
            out = new FileOutputStream(path);
            new SimpleQrcodeGenerator().generate(url).toStream(out);
        } finally {
            IOUtils.closeQuietly(out);
        }
        return  path;
    }
    /**
     * 生成带图片的二维码
     * 在线图片
     *//*
    public static String qrcode(String imgUrl,String url) throws Exception {
        OutputStream out = null;

        try {
            String newUrl = FilePathUtil.setFilePath();
            out = new FileOutputStream(newUrl);
            // 生成图片的绝对引用地址
            String picUrl = StringUtils.replace(StringUtils.substringAfter(newUrl, ProProperties.getInstance().getBaseUploadURL()),"\\", "/");
            new SimpleQrcodeGenerator().setRemoteLogo(imgUrl).generate(url).toStream(out);
            return picUrl;

        } finally {
            IOUtils.closeQuietly(out);
        }
    }
*/
    public static void main(String[] args) {
        //QrcodeUtil.qrcode("http://m.mestesso.net/weiXin/oauth/1","E:\\12.png");
        String decodeImgPath = "D:/1.png";
        String encodeImgPath = "D:/2.png";
        String contents = "20190309000000001";
        int width = 150, height = 40;
        QrcodeUtil.encode(contents, width, height, encodeImgPath);
    }
}
